package prv.pursuivant.config.impl;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

import prv.pursuivant.core.AchievType;
import prv.pursuivant.core.AchievTypesTree;

import com.google.inject.Guice;
import com.google.inject.Injector;

public class XStreamCategorySerializerReadingFileTest {

	private final XStreamCategorySerializer serializer;

	public XStreamCategorySerializerReadingFileTest(
			XStreamCategorySerializer serializer) {
		this.serializer = serializer;
		// TODO Auto-generated constructor stub
	}

	/**
	 * @param args
	 * @throws IOException
	 */
	public static void main(String[] args) throws IOException {
		Injector injector = Guice
				.createInjector(new XStreamCategorySerializerModule());
		XStreamCategorySerializer serializer = injector
				.getInstance(XStreamCategorySerializer.class);
		new XStreamCategorySerializerReadingFileTest(serializer).test();
	}

	private void test() throws IOException {
		File file = new File("runWD/config/category.168.xml");
		File fileOut = new File("testWD/config/category.168.xml");
		FileInputStream fis = new FileInputStream(file);
		AchievTypesTree tree = serializer.readTree(fis);
		fis.close();
		echo(tree, tree.getRootId(), "");
		FileOutputStream fos = new FileOutputStream(fileOut);
		serializer.writeTree(tree, fos);
		fos.close();
		fis = new FileInputStream(file);
	}

	private void echo(AchievTypesTree tree, String id, String stack) {
		AchievType achiev = tree.findAchievTypeById(id);
		System.out.println(stack + achiev + ":");
		for (String crit : achiev.getCriteria()) {
			echo(tree, crit, stack + "  ");
		}
	}
}
